package com.room.controller.user;

import com.room.mapper.UserMapper;
import com.room.pojo.DormResult;
import com.room.pojo.Role;
import com.room.pojo.User;
import com.room.service.RoleService;
import com.room.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/8
 * @描述
 */
@RestController
@RequestMapping("/user/manager")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    /**
     * 获取当前用户拥有的角色
     * @param userId
     * @return
     */
    @RequestMapping(value = "/RoleinitData",method = RequestMethod.GET)
    public Map<String,Object> getRoleByUserId(@PathVariable Long userId){
            Map<String,Object> resultMap = new HashMap<String,Object>();
            List<Role> roleInit = userService.getRoleByUserId(userId);
        resultMap.put("roleInit",roleInit);
        return resultMap;
    }

    @RequestMapping("/id/{userId}")
    public User getUserById(@PathVariable Long userId){
        return userService.getUserById(userId);
    }
    public DormResult updatePassword(Long userId, String oldPassword, String newPassword){
          return null;
    }

    @RequestMapping(value="/{userId}",method = RequestMethod.PUT)
    public DormResult deleteUser(@PathVariable Long userId){
        if(userService.deleteUser(userId)==1){
            return DormResult.ok("删除成功");
        }
        return DormResult.error("删除失败");
    }

    public DormResult updateUser(User user){
        if(userService.updateUser(user)==1){
            return DormResult.ok("更新成功");
        }
        return DormResult.error("更新失败");
    }

    @RequestMapping(value = "/roles",method = RequestMethod.PUT)
    public DormResult updateUserRoles(Long userId,Long[] rids){
        if(userService.updateUserRoles(userId,rids)==rids.length){
            return DormResult.ok("更新成功");
        }
        return DormResult.error("更新失败");
    }

    @RequestMapping("/{keywords}")
    public List<User> getUsersByKeywords(@PathVariable(required = false)String keywords){
        List<User> users = userService.getUsersByKeywords(keywords);
        return users;
    }

    @RequestMapping(value = "/roles",method = RequestMethod.GET)
    public List<Role> allRoles(){
        return roleService.roles();
    }


}
